package com.loubii.sunshineSchool.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.pojo.SSMsg;

import java.util.List;

public class SSMsgAdapter extends RecyclerView.Adapter<SSMsgAdapter.ViewHolder> {

    private List<SSMsg> mSsMsgList;

    static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout leftLayout;

        LinearLayout rightLayout;

        TextView leftMsg;

        TextView rightMsg;

        public ViewHolder(View view) {
            super(view);
            leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
            leftMsg = (TextView) view.findViewById(R.id.left_msg);
            rightMsg = (TextView) view.findViewById(R.id.right_msg);
        }
    }

    public SSMsgAdapter(List<SSMsg> ssMsgList) {
        mSsMsgList = ssMsgList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SSMsg ssMsg = mSsMsgList.get(position);
        if (ssMsg.getType() == SSMsg.TYPE_RECEIVED) {
 
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(ssMsg.getContent());
        } else if(ssMsg.getType() == SSMsg.TYPE_SENT) {
 
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(ssMsg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mSsMsgList.size();
    }

}
